﻿/// <reference path="../jquery-3.6.0.js" />


$(function () {
    var paysum = 0;

    $(".shdz .list .item").click(function () {
        var tis = $(this);
        $(".shdz .list .item").removeClass("sel");
        tis.addClass("sel");
        setBtmAddr(
            tis.attr("data-proe"),
            tis.attr("data-city"),
            tis.attr("data-area"),
            tis.attr("data-det"),
            tis.attr("data-mob"),
            tis.attr("data-cons"));
    });

    $("#paytb .input-nud input").on("change", function () {
        var vlue = parseInt($(this).val());
        if (!vlue) {
            $(this).val("1");
            vlue = 1;
        }
        var sum = $(this).parents("tr:first").find(".tr-sum");
        sum.text("￥" + vlue + ".00");
        var nds = $("#paytb .input-nud input");
        var sum = 0;
        for (var i = 0; i < nds.length; i++) {
            var s = $(nds.get(i));
            sum += parseInt(s.val());
        }
        paysum = sum;
        $("#sumpric").text(sum + ".00");
    });

    $("#use-newaddr").on("click", function () {
        showAddrForm()
    });

    $("#submit").on("click", function () {
        //没有收货地址则要求先添加收货地址
        if ($(".shdz .list > .item").length == 0) {
            layer.msg("请先添加收货地址!",
                {
                    icon: 7,
                    time: 1000,
                    area: "200px",
                    end: function () {
                        showAddrForm();
                    }
                });
            return;
        }
        //余额不足提示充值
        if (paysum > MyUser.remsum) {
            layer.confirm('账户余额不足,是否立即充值?',
                {
                    icon: 3,
                    title: '余额不足',
                }, function (index) {
                    Tools.showTopUp();
                    layer.close(index);
                });
            return;
        }
        //获取选择的地址id
        var addrid = $(".shdz .list > .sel").attr("data-reid");
        var $itms = $("#paytb .rhitem");
        var bys = [];
        for (var i = 0; i < $itms.length; i++) {
            var $dom = $($itms.get(i));
            var bitm = {
                rushid: parseInt($dom.attr("data-rhid")),
                bycount: parseInt($dom.find(".input-nud").children("input").val())
            }
            bys.push(bitm);
        }
        var postobj = {
            seladdr: addrid,
            bys: bys
        }
        var layload = layer.load(2, {
            offset: ["337px", "750px"]
        });
        $.post(
            {
                url: "/pay/pay",
                data: { byinfo: JSON.stringify(postobj) },
                dataType: "json",
                success: function (result) {
                    if (result.Isucc) {
                        layer.alert('恭喜,交易成功!',
                            {
                                icon: 1,
                                btn: ["查看我的云购", "返回首页"],
                                title: ["交易成功", "height:30px;line-height:30px"],
                                closeBtn: 0,
                                btn1: function () {
                                    window.location = "/userinfo/myyg";
                                },
                                btn2: function () {
                                    window.location = "/";
                                }
                            });
                    }
                    else {
                        layer.msg("订单提交失败了,请刷新页面后重试!", {
                            time: 3000,
                            icon: 5
                        });
                    }
                },
                complete: function () {
                    layer.close(layload);
                }
            });
    });

    $.get({
        url: "/SpAddr/AddrForm",
        dataType: "html",
        success: function (content) {
            $("#paysection").append(content);
            $("#addrpostform").hide();
        }
    });

    function showAddrForm() {
        var dom = $("#addrpostform");
        layer.open({
            type: 1,
            content: dom,
            title: "&#xe610 使用新地址",
            area: ["450px", "460px"],
            skin: "layer-newaddr",
            resize: false,
            success: function () {
                dom.show();
                dom.ajaxForm({
                    dataType: "json",
                    success: function (data) {
                        if (data.Isucc) {
                            layer.msg("地址添加成功!",
                                {
                                    icon: 1,
                                    time: 2000,
                                    area: "100px",
                                    end: function () {
                                        location.reload();
                                    }
                                });
                        }
                        else {
                            layer.msg("地址添加失败!",
                                {
                                    icon: 2,
                                    time: 2000,
                                    area: "100px",
                                });
                        }
                    }
                })
            },
            end: function () {
                dom.hide();
            }
        });
    }

    function setBtmAddr(proe, city, area, det, mob, cons) {
        $("#zjsz").text(`${proe} ${city} ${area + det}`);
        $("#shr").text(`${cons}  ${mob}`);
    }
})